<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use \DB;
use \Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    //栏目首页
    public function getIndex(Request $request){
            $sessiondata = $request->session()->all();
            $sessionid=$sessiondata['id'];
            $sessionusername=$sessiondata['username'];
            $sessionres=DB::table('adminuser')->where('id',$sessionid)->first();
        $this->getCatesByPid(0);
        $cates=$this->getCates();
        return view('admin.cate.index',['cates'=>$cates,'usersessioninfo'=>$sessionres]);
    
    }

    public function getCatesByPid($catepid){
      $res=DB::table('admincate')->where('catepid',$catepid)->get(); 
      $data=[];
      foreach ($res as $key => $value) {
           $value->sub=$this->getCatesByPid($value->id);
           $data[]=$value;
       } 
       return $res;
    }



    public function getCates(){
        $res=DB::table('admincate')
        ->select(DB::raw('*,concat(catepath,",",id) as paths'))
        ->orderBy('paths')
        ->get();
        foreach($res as $k=>$v){
            $tmp=explode(',',$v->catepath);
            $len=count($tmp)-1;
            $res[$k]->catename=str_repeat('|------',$len).$v->catename;
        }
        return $res;
     }


    // 栏目添加页面
    public function getAdd(Request $request){
        $sessiondata = $request->session()->all();
                  $sessionid=$sessiondata['id'];
                  $sessionusername=$sessiondata['username'];
                  $sessionres=DB::table('adminuser')->where('id',$sessionid)->first();
         $cates=$this->getCates();
        return view('admin.cate.add',['cates'=>$cates,'usersessioninfo'=>$sessionres]);
    }

    /**
     * ajax检测栏目名称是否存在
     */
    public function catetestone(Request $request)
    {
        $catename=$request->input('catename');
        $res=DB::table('admincate')->where('catename',$catename)->first();
        if($res){
            // 为真 用户名已存在 返回 1 不能使用
            echo 1;
        }else{
            // 为假 用户名不存在 返回 0 可使用
            echo 0;
        }
    }
    /**
     * ajax检测栏目目录是否存在
     */
    public function catetesttwo(Request $request)
    {
        $catenames=$request->input('catenames');
        $res=DB::table('admincate')->where('catenames',$catenames)->first();
        if($res){
            // 为真 用户名已存在 返回 1 不能使用
            echo 1;
        }else{
            // 为假 用户名不存在 返回 0 可使用
            echo 0;
        }
    }

    // 栏目添加的插入操作
    public function postInsert(Request $request)
    {

        // laravel表单验证
		 $this->validate($request, [	
	        'catename' => 'required',
	        'catenames' => 'required|regex:[^(?!\d+$)[\da-zA-Z]{4,10}$]',
	        'catetype'=> 'required',
	       
	    ],[
	    	'catename.required'=>'栏目名称不能为空',
	    	'catenames.regex'=>'栏目目录格式不正确,为4-10位字母数字',
	    	'catenames.required'=>'密码不能为空',
	    	'catetype.required'=>'栏目模型不能为空',
	    ]);	

   		 $data=$request->only(['catename','catepid','catenames','cateimage','cateshow','catetype']);

        $pid=$request->input('catepid');
        if($pid==0){
            $data['catepath']=0;
        }else{
            $info=DB::table('admincate')->where('id',$pid)->first();
            $data['catepath']=$info->catepath.','.$info->id;
        }
        // 后期要修改的数据
        $data['cateid']='0001';
        $data['cateurl']='0001';
        $data['catediysort']='0';
        $data['catemesnum']='0';
        // dd($data);



        $res=DB::table('admincate')->insert($data);
        if($res){
            return redirect('/admin/cate/index')->with('success','数据添加成功');
        }else{
            return back()->with('error','数据插入失败');
        }


    }

}
